local skel = fk.CreateSkill {
  name = "emo__xuexu",
}

Fk:loadTranslationTable{
  ["emo__xuexu"] = "削銊",
  [":emo__xuexu"] = "摸牌阶段，你可以改为将非【杀】的“銊”置入弃牌堆，然后将牌堆顶五张牌扣置武将牌上称为“銊”；你可以将任意张“銊”当无距离限制的【杀】使用或打出。底牌中每有一张【杀】则此【杀】伤害+1，此【杀】对目标造成伤害后，底牌每有一张非【杀】，你失去一点体力。",

  ["$emo__xuexu_pile"] = "銊",
  ["#emo__xuexu"] = "削銊：你可以将任意张“銊”当无距离限制的【杀】使用或打出",
  ["#emo__xuexu-invoke"] = "削銊：你可以跳过摸牌阶段，改为将牌堆顶 5 张牌置入“銊”",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#emo__xuexu",
  derived_piles = "$emo__xuexu_pile",
  interaction = function (self, player)
    return UI.Spin{ from = 1, to = #player:getPile("$emo__xuexu_pile") }
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local c = Fk:cloneCard("slash")
    c.skillName = skel.name
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local num = self.interaction.data
    if num == nil then return "" end
    local cards = table.random(player:getPile("$emo__xuexu_pile"), num)
    local slashNum = #table.filter(cards, function (id)
      return Fk:getCardById(id, true).trueName == "slash"
    end)
    local nonSlashNum = num - slashNum
    if slashNum > 0 then
      use.additionalDamage = (use.additionalDamage or 0) + slashNum
    end
    if nonSlashNum > 0 then
      use.extra_data = use.extra_data or {}
      use.extra_data.emo__xuexu_nonSlashNum  =  nonSlashNum
    end
    -- 自己能看到底牌，别人看不到
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonPut, skel.name, nil, false, player, nil, {})
    use.card:addSubcards(cards)
  end,
  enabled_at_play = function(self, player)
    return #player:getPile("$emo__xuexu_pile") > 0
  end,
  enabled_at_response = function (self, player, response)
    return #player:getPile("$emo__xuexu_pile") > 0
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and player.phase == Player.Draw and target == player and not data.phase_end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__xuexu-invoke" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.phase_end = true
    local throw = table.filter(player:getPile("$emo__xuexu_pile"), function(id) return Fk:getCardById(id).trueName ~= "slash" end)
    if #throw > 0 then
      room:moveCardTo(throw, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name)
      if player.dead then return end
    end
    player:addToPile("$emo__xuexu_pile", room:getNCards(5), false, skel.name)
  end,
})

skel:addEffect(fk.Damage, {
  is_delay_effect = true,
  anim_type = "negative",
  can_trigger = function (self, event, target, player, data)
    return target == player and not player.dead and data.by_user and data.card and table.contains(data.card.skillNames, skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if use_event then
      local use = use_event.data
      local num = (use.extra_data or {}).emo__xuexu_nonSlashNum
      if num then
        room:loseHp(player, num, skel.name)
      end
    end
  end,
})

skel:addEffect("visibility", {
  card_visible = function(self, player, card)
    local owner = Fk:currentRoom():getCardOwner(card)
    if owner and owner:getPileNameOfId(card.id) == "$emo__xuexu_pile" then
      return false
    end
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function(self, player, skill, card, to)
    return card and table.contains(card.skillNames, skel.name)
  end,
})

return skel
